import 'package:flutter/services.dart';

import '../terms.dart';

/// 读取asset文件夹下面的内容
Future<String> loadAsset(String fileName) async {
  var a = await rootBundle.loadString(fileName);
  return a;
}

/// 将数字转化为汉字，100以内(包含100)
String numberToChinese(int number) {
  const String chineseNumbers = '零一二三四五六七八九十';
  const String chineseUnits = ' 十百';
  String result = '';
  int unitIndex = 0;
  if (number == 0) {
    result = '一';
    return result;
  }
  while (number > 0) {
    int digit = number % 10;
    if (digit != 0 || unitIndex == 0 || result.isNotEmpty) {
      result = chineseNumbers[digit] + chineseUnits[unitIndex] + result;
    }
    unitIndex++;
    number = (number / 10).floor();
  }
  return result.trim();
}

Color getColorFromHex(String hexColor) {
  hexColor = hexColor.replaceAll("#", "");
  if (hexColor.length == 6) {
    hexColor = "FF$hexColor"; // 添加透明度（不透明）
  }
  return Color(int.parse(hexColor, radix: 16));
}

/// 获取棋盘上所有非空棋子的坐标
List<ChessStep> getNonZeroChessSteps(Board board) {
  final grid = board.getChessBoard();
  List<ChessStep> steps = [];
  for (int i = 0; i < board.getBoardSize(); i++) {
    for (int j = 0; j < board.getBoardSize(); j++) {
      if (grid[i][j] != 0) {
        steps.add(ChessStep(j + 1, 20 - (i + 1), grid[i][j]));
      }
    }
  }
  return steps;
}

/// 上下左右4个位置的棋谱
List<ChessStep> nearChess(Board board, ChessStep chessStep) {
  // printInfo('进入打吃、扑的判断');
  int x = chessStep.x.toInt();
  int y = chessStep.y.toInt();
  int bw = chessStep.bw;

  bool isValid(int nx, int ny) {
    if (nx > 0 && ny > 0 && nx <= 19 && ny <= 19) {
      int value = board.getChessValue(nx, ny);
      return value > 0 && value != bw;
    }
    return false;
  }

  List<List<int>> directions = [
    [0, -1],
    [0, 1],
    [-1, 0],
    [1, 0],
  ];

  return directions
      .map((d) => ChessStep(x + d[0], y + d[1], 3 - bw))
      .where((step) => isValid(step.x, step.y))
      .toList();
}

/// 获取指定位置四周的棋子
/// [board]: 棋盘
/// [chessStep]: 当前棋步
/// [mode]: 模式选择
List<ChessStep> getNearChess(
  Board board,
  ChessStep chessStep, {
  ColorMode mode = ColorMode.all,
}) {
  int x = chessStep.x;
  int y = chessStep.y;
  int bw = chessStep.bw;

  bool isValid(int nx, int ny) {
    if (nx > 0 && ny > 0 && nx <= 19 && ny <= 19) {
      int value = board.getChessValue(nx, ny);
      if (value <= 0) return false; // 空位置

      switch (mode) {
        case ColorMode.all:
          return value > 0; // 所有棋子
        case ColorMode.different:
          return value != bw; // 异色棋子
        case ColorMode.same:
          return value == bw; // 同色棋子
      }
    }
    return false;
  }

  List<List<int>> directions = [
    [0, -1],
    [0, 1],
    [-1, 0],
    [1, 0],
  ];

  // 根据模式确定棋子的颜色
  int targetBw = (mode == ColorMode.different) ? 3 - bw : bw;

  return directions
      .map((d) => ChessStep(x + d[0], y + d[1], targetBw))
      .where((step) => isValid(step.x, step.y))
      .toList();
}

/// 计算当前棋步和其他位置之间距离的平方
int distanceSquared(ChessStep step, int xx, int yy) {
  int x = changedX(step);
  int y = changedY(step);
  return (x - xx) * (x - xx) + (y - yy) * (y - yy);
}

int distanceSquared2(ChessStep step, int xx, int yy) {
  int x = step.x;
  int y = step.y;
  return (x - xx) * (x - xx) + (y - yy) * (y - yy);
}

int changedX(ChessStep step) {
  return step.y;
}

int changedY(ChessStep step) {
  return 20 - step.x;
}

bool isContain(String desc) {
  if (desc.contains('拆边') ||
      desc.contains('挂角') ||
      desc.contains('拦') ||
      desc.contains('夹击')) {
    return true;
  }
  return false;
}

/// 去重
String removeSame(String s) {
  return s.split(',').toSet().join(',');
}

/// 判断一个字符串数组和另外一个字符串是否有相同的元素
int hasCommonElement(List<String> array, String s) {
  List<String> stringItems = s.split(',');

  for (String item in array) {
    if (stringItems.contains(item)) {
      return array.indexOf(item);
    }
  }
  return -1;
}
